<?php

declare(strict_types=1);

namespace App\Model;

use Hyperf\DbConnection\Model\Model;
use Hyperf\DbConnection\Db;

/**
 * 评论点赞
 */
class CommentLike extends Model
{
    protected $table = 'moment_comment_like';

    protected $fillable = [
        "user_id", "moment_id", "comment_id",  "flag"
    ];

    /**
     * 添加点赞记录
     */
    static function addLike($userId = 0, $commentId = 0, $flag = 1, $momentId = 0)
    {
        $ret = self::updateOrCreate([
            'user_id' => $userId,
            'comment_id' => $commentId,
        ], [
            'user_id' => $userId,
            'comment_id' => $commentId,
            'moment_id' => $momentId,
            'flag' => $flag
        ]);
        return $ret ? 1 : 0;
    }

    /**
     * 是否点赞评论
     */
    static function checkLike($userId = 0, $commentId = 0)
    {
        $where = [
            'user_id' => $userId,
            'comment_id' => $commentId,
            'flag' => 1
        ];
        return self::where($where)->first() ? 1 : 0;
    }
}
